package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public List<AreaVO> queryProvinces() {
        List<AreaVO> areaVOS = areaRepository.queryProvinces();
        return areaVOS;
    }

    @Override
    public List<AreaVO> queryChildren(Integer parentId) {
        List<AreaVO> areaVOS = areaRepository.queryChildren(parentId);
        return areaVOS;
    }

    @Override
    public List<AreaVO> tree() {
        List<AreaVO> areaVOS = areaRepository.queryProvinces();
        log.debug("查询所有省份areaVOS:{}", areaVOS);
        //查询所有省份的子区域并为每个省份添加子区域
        for (AreaVO areaVO : areaVOS) {
            List<AreaVO> childrenVOS = queryChildren(areaVO.getId().intValue());
            areaVO.setChildren(childrenVOS);
        }
        for (AreaVO areaVO : areaVOS) {
            for (AreaVO child : areaVO.getChildren()) {
                List<AreaVO> childrenVOS = queryChildren(child.getId().intValue());
                child.setChildren(childrenVOS);
            }
        }
        log.debug("添加子区域后areaVOS:{}", areaVOS);
        return areaVOS;
    }
}
